# import pyttsx3
# engine = pyttsx3.init()
# # engine.setProperty('volume', 0.5)  # 设置音量为50%
# engine.setProperty('rate', 160)  # 设置语速为120WPM
# # voices = engine.getProperty('voices')
# # engine.setProperty('voice',  voices[0].id)  #  0是女声，1只能读英文，无法切换到男生。
# # engine.setProperty('voice', 'en+m7')
# # engine.setProperty('voice', 'zh-CN')
# engine.setProperty('voice', 'Zira')

from tts_engine import SystemEngine
Tts = SystemEngine()
Tts.set_parameters(rate = 160)

# engine.setProperty('rate', 160)  # 设置语速为120WPM


def read(text):

    Tts.speak(text)
    #注意：在linux系统中需要安装espeak和ffmpeg库才能保存语音文件
    # engine.save_to_file(text, 'test.mp3')
    # engine.stop()

def readFile():
    # f = open(r"example.txt","r",encoding="utf-8")
    # print(f.read())
    readText = ''
    with open("example.txt","r",encoding="utf-8") as f:
        lines = f.readlines()
        for line in lines:
            print('----------line')
            print(line)
            # Tts.speak(line)
            readText += line
    #         #
    print(readText)
    # Tts.speak(readText)
    Tts.synthesize(readText)

if __name__ == '__main__':
    # supportList()
    readFile()

# 获取支持的语音引擎列表：
# def supportList():
#
#     voices = engine.getProperty('voices')
#     for voice in voices:
#         print("Name: %s" % voice.name)
#         print("ID: %s" % voice.id)
#         print("Age: %s" % voice.age)
#         print("Gender: %s" % voice.gender)
#         print("Languages Known: %s" % voice.languages)


# def onStart(name):
#     print('开始说话:', name)
#
# def onEnd(name, completed):
#     print('说话结束:', name, completed)
#
# engine.connect('started-utterance', onStart)
# engine.connect('finished-utterance', onEnd)


#
# import pyttsx3
# import requests
# from bs4 import BeautifulSoup
# engine = pyttsx3.init('sapi5')
# voices = engine.getProperty('voices')
# newVoiceRate = 130
# ## Reduce The Speech Rate
# engine.setProperty('rate',newVoiceRate)
# engine.setProperty('voice', voices[1].id)
# def speak(audio):
#     engine.say(audio)
#     engine.runAndWait()
#     text = str(input("Paste article\n"))
#     res = requests.get(text)
#     soup = BeautifulSoup(res.text, 'html.parser')
#     articles = []
#     for i in range(len(soup.select('.p'))):
#         article = soup.select('.p')[i].getText().strip()
#         articles.append(article)
#
#     text = " ".join(articles)
#     speak(text)

# engine.save_to_file(text, 'test.mp3')
## If you want to save the speech as a audio fileengine.runAndWait()